﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SSharp.Core.Evaluator {
	public static partial class Eval {
		/// <summary>
		/// Evaluates if expressions
		/// (if condition
		///	    consequent
		///	    alternate)
		/// </summary>
		private class IfEvaluator : SpecialFormEvaluator {
			public IfEvaluator() : base("if") { }

			public override EvaluationResult Evaluate(object[] parameters, IEnvironment env) {
				if (parameters.Length < 2 || parameters.Length > 3) {
					throw new SyntaxError("if accepts two or three parameters. Given " + parameters.Length);
				}

				object condition = parameters[0];
				object consequent = parameters[1];
				object alternate = parameters.Length > 2 ? parameters[2] : null;

				object conditionResult = Eval.Evaluate(condition, env);
				if (!(conditionResult is bool)) {
					throw new TypeError("if condition must be a booelan; given: " + conditionResult);
				}

				if ((bool)conditionResult) {
					return new MoreEvaluationNeeded(consequent, env);
				} else {
					return new MoreEvaluationNeeded(alternate, env);
				}
			}
		}
	}
}
